#ifndef _DOWNLOAD_H_
#define _DOWNLOAD_H_

#include <stdio.h>
#include "function.h"
#include "api.h"
#include <semaphore.h>
#include <jni.h>
#include "dvb.h"

#define INFO_REQ_TAG 					0x0080
#define DATA_REQ_TAG 					0x0082

#define SM1670_OPERATOR_REQUEST_TAG				0x0010
#define SM1670_OPERATOR_RESPONSE_TAG			0x0011
#define SM1670_UPDATE_INFO_REQUEST_TAG		0x0012
#define SM1670_UPDATE_INFO_RESPONSE_TAG		0x0013
#define SM1670_DOWNLOAD_TRIGGER_TAG			0x0014
#define SM1670_DOWNLOAD_STATUS_TAG			0x0015
#define SM1670_DOWNLOAD_CORRUPTED_TAG		0x0016

#define TRIGGER_RESERVED_TAG				0x00
#define TRIGGER_NO_DOWNLOAD_TAG				0x01
#define TRIGGER_START_RINGHTNOW_TAG			0x02
//#define TRIGGER_RESERVED_TAG				0x03

#define TF_DOWNLOAD_INFO_REQUEST			0x0020
#define TF_DOWNLOAD_DATA_REQUEST			0x0022
#define DOWNLOAD_FINISH_TAG					0x0026
#define DOWNLOAD_GET_DATE_TIME_TAG	 		0x00A7
#define CRC_CHECK							0x0100


//#define INFO_RESPONE					0X0081
#define DOWNLOAD_INFO_RESPONE			0X0021
//#define DATA_BLOCK						0X0083
#define DOWNLOAD_DATA_RESPONE				0X0023
#define DL_CANCEL_TAG					0X0084
#define DL_DEVICE_INFO_REQUEST			0x0085
#define DL_DEVICE_INFO_RESPONE		0x0086

#define DOWNLOAD_DATE_TIME_RESP_SUB_TAG 0x00A8
#define DLD_TAG_VERSION_RESP				0x0086
#define DLD_TAG_VERSION_RESP_SUB 		0x00AA
//#define REQ_SM1670_VER_INFO_TAG			0x0085
//#define REQ_SM1670_VER_INFO_SUB_TAG	0x00A9


#define MAX_MESSAGE_SIZE		0x1200
#define ADAPTATION_LENGTH_INDEX	0x09
#define MESSAGE_LENGTH_INDEX	0x0A
#define CRC16_TAG               0x0100

#define COM_PATIBILITY_DES_INDEX        0x1C

#define DII_MODULE_COUNT_INDEX          0x37
#define REQUEST_MESSAGE_DDMID_INDEX		0x14	//12+2位标志位
#define REQUEST_MESSAGE_DDBID_INDEX		(REQUEST_MESSAGE_DDMID_INDEX + 2)
#define IRDETO_LOADER 0
#define SSU_LOADER 1
#define DOWNLOAD_FINISH 	0x0000
extern sem_t *g_update_signal;
//extern sem_t *g_update_Recv_signal;

#include "android/log.h"

#define LOG_LOADER_TAG "SmitUpdate"
#define LOG_INFO_TAG "SmitUpdateInfo"
#define LOGE(...) __android_log_print(ANDROID_LOG_INFO  , LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO  , LOG_TAG, __VA_ARGS__)
#define LOGV(...) __android_log_print(ANDROID_LOG_INFO  , LOG_TAG, __VA_ARGS__)

unsigned char g_get_1670buf[1024];

typedef struct {
	BOOL SdlOpen;
	BOOL CAType;
}DongleUpdateControl;

typedef struct {
	unsigned char* IrdeoType;
	unsigned char* SSUType;
}DongleUpdateDongleType;

typedef enum
{
	Q_NONE,
	Q_START,
	Q_TIME,
	Q_DATA,
	Q_SIZE,
	Q_REOPTION,
	Q_MIXTERM_VER,
	Q_MANUFACTURER_TYPE_HWVER,
	Q_TIMEOUT,
	Q_TRANSFER_END,
	Q_DOWNLOAD_ERROR,
	Q_DOWNLOAD_OK,
	Q_SSU_INFO_REQUEST,
	Q_SSU_DATA_REQUEST,
	Q_SSU_FINISH,
	Q_SSU_DOWNLOAD_DATE_TIME,
	Q_SSU_SM1670_INFO,
	Q_UPDATE_START
}RequestOption;

#define MAX_COMMAND_SIZE 0x8100
unsigned char s_abCommand[MAX_COMMAND_SIZE];
static unsigned char s_abPayload[MAX_COMMAND_SIZE];
static unsigned long s_abCommandLen;
static const char *bpMixtermVer = "4.0.4";
typedef int (*pfLoaderCallback)(DVB_CB_TYPE_T eType, int msg, void* param);

long loaderUpdateTask(void);
int loaderSasReadTask(void);
int loaderReadCommandOption(unsigned char *pbCommand, unsigned long dwBufferSize, unsigned long *pdwCommandLen);
int loaderSendUpdateFirmWareInfo(unsigned char *id);
int loaderSendUpdateProcessMsg(int type);
void loaderSetFirmwareInfo(char* sn, char* filePath);
int loaderInit(pfLoaderCallback pfCb);

#endif
